<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { type FormInstance, type FormRules } from "element-plus"
import { User, Lock } from "@element-plus/icons-vue"
import { type LoginRequestData } from "@/api/login/types/login"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import Owl from "./components/Owl.vue"
import { useFocus } from "./hooks/useFocus"
// import axios from "axios"

// const optionsEn = reactive({
//   method: "POST",
//   headers: { "content-type": "application/json" },
//   data: {
//     password: "admin",
//     userName: "admin"
//   },
//   url: "http://47.120.67.75:8082/user/login"
// })
// axios(optionsEn)
//   .then((res) => {
//     console.log("res==", res)
//   })
//   .catch(function (error) {
//     // 请求失败处理
//     console.log(error)
//   })

const router = useRouter()
const { isFocus, handleBlur, handleFocus } = useFocus()

/** 登录表单元素的引用 */
const loginFormRef = ref<FormInstance | null>(null)

/** 登录按钮 Loading */
const loading = ref(false)
/** 登录表单数据 */
const loginFormData: LoginRequestData = reactive({
  user_tag: "admin",
  password: "admin"
  // code: ""
})
/** 登录表单校验规则 */
const loginFormRules: FormRules = {
  user_tag: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 1, max: 16, message: "长度在 8 到 16 个字符", trigger: "blur" }
  ]
}
//注册
const registerData = reactive({
  name: "",
  password: ""
})
const showRegister = ref(false)
/** 登录逻辑 */
const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean, fields) => {
    if (valid) {
      loading.value = true
      useUserStore()
        .login(loginFormData)
        .then(() => {
          router.push({ path: "/" })
        })
        .catch(() => {
          // createCode()
          loginFormData.password = ""
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      console.error("表单校验不通过", fields)
    }
  })
}

const handleRegisterLogin = () => {
  showRegister.value = false
}

// const handleRegister = () => {
//   showRegister.value = true
// }

const handleRegisterConfirm = () => {
  showRegister.value = false
}

/** 初始化验证码 */
// createCode()
</script>

<template>
  <div class="login-container">
    <ThemeSwitch class="theme-switch" />
    <Owl :close-eyes="isFocus" />
    <div class="login-card">
      <div class="title">
        <!-- <img src="@/assets/layouts/logo-text-2.png" /> -->
        <h1>学生请假管理系统</h1>
      </div>
      <div class="content" v-if="showRegister">
        <el-form ref="loginFormRef">
          <el-form-item prop="user_tag">
            <el-input
              v-model.trim="registerData.name"
              placeholder="用户名"
              type="text"
              tabindex="1"
              :prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="registerData.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              size="large"
              show-password
              @blur="handleBlur"
              @focus="handleFocus"
            />
          </el-form-item>
          <el-button style="margin-left: 0px" type="primary" size="large" @click="handleRegisterConfirm"
            >注册</el-button
          >
          <el-text
            style="margin: 16px 0; display: block; cursor: pointer"
            class="mx-1"
            type="primary"
            @click="handleRegisterLogin"
            >去登录</el-text
          >
        </el-form>
      </div>
      <div class="content" v-else>
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="user_tag">
            <el-input
              v-model.trim="loginFormData.user_tag"
              placeholder="用户名"
              type="text"
              tabindex="1"
              :prefix-icon="User"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginFormData.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              size="large"
              show-password
              @blur="handleBlur"
              @focus="handleFocus"
            />
          </el-form-item>
          <el-button :loading="loading" type="primary" size="large" @click.prevent="handleLogin">登录</el-button>
          <!-- <el-text
            style="margin: 16px 0; display: block; cursor: pointer"
            class="mx-1"
            type="primary"
            @click="handleRegister"
            >去注册</el-text
          > -->
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }
  .login-card {
    width: 480px;
    max-width: 90%;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: #fff;
    overflow: hidden;
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 150px;
      img {
        height: 100%;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 40px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button {
        width: 100%;
        margin-top: 10px;
      }
    }
  }
}
</style>
